package com.xunwulian.dubbo.service;

import com.xunwulian.base.entity.SmsGprsBalanceEntity;
import org.apache.http.util.Asserts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/1/12.
 */
public class GprsSmsFileTask implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(GprsSmsFileTask.class);

    // 卡信息服务
    private InnerGprsSmsService innerGprsSmsService;

    // 卡文件导出的卡信息集合
    private List<SmsGprsBalanceEntity> list;

    private Integer start;

    private Integer end;

    public GprsSmsFileTask(InnerGprsSmsService innerGprsSmsService,
                           List<SmsGprsBalanceEntity> list,
                           int start,
                           int end) {

        Asserts.notNull(list, "list can not be null");

        this.innerGprsSmsService = innerGprsSmsService;
        this.list = list;
        this.start = start;
        this.end = end > list.size() ? list.size() : end;
    }

    /**
     * 单个线程实现rollback
     */
    @Override
    @Transactional
    public void run() {
        long timeA = System.currentTimeMillis();
        for (int i = start; i < end; i++) {

            SmsGprsBalanceEntity info = list.get(i);

            // 更新如下字段：卡号	卡号备注	IMSI	ICCID	用户状态	工作状态	本月累计使用流量(MB)	本月累计使用短信(条)
            SmsGprsBalanceEntity entity = innerGprsSmsService.loadByDateMsisdn(info.getMsisdn(), info.getDate());
            if (entity != null) {
                innerGprsSmsService.updateCardInfo4Import(info);
            } else {
                innerGprsSmsService.addCardInfo4Import(info);
            }
        }

        logger.info("" + (System.currentTimeMillis() - timeA) / 1000);
    }
}
